import torch.nn as nn
def vgg_block(num_convs, in_channels, out_channels):# 定义VGG块
    layers = []
    for i in range(num_convs):
        layers.append(nn.Conv2d(in_channels, out_channels, kernel_size=3, padding=1))
        layers.append(nn.ReLU(inplace=True))
        in_channels = out_channels
    layers.append(nn.MaxPool2d(kernel_size=2, stride=2))
    return nn.Sequential(*layers)

class ModularVGG11(nn.Module):
    def __init__(self, num_classes=11):
        super(ModularVGG11, self).__init__()
        # VGG11配置: (卷积层数, 输出通道数)
        config = [(1, 16), (1, 32), (2, 64), (2, 128), (2, 128)]

        layers = []
        in_channels = 3
        for num_convs, out_channels in config:
            layers.append(vgg_block(num_convs, in_channels, out_channels))
            in_channels = out_channels

        self.features = nn.Sequential(*layers)
        self.classifier = nn.Sequential(
            nn.Linear(2048, 256),
            nn.ReLU(inplace=True),
            nn.Dropout(0.5),
            nn.Linear(256, 256),
            nn.ReLU(inplace=True),
            nn.Dropout(0.5),
            nn.Linear(256, num_classes),
        )

    def forward(self, x):
        x = self.features(x)
        x = x.view(x.size(0), -1)
        x = self.classifier(x)
        return x

# 打印模型结构
if __name__ == "__main__":
    model = ModularVGG11()
    print(model)